// pages/component/popup.js
import {
  fileUrl,
} from "../../network/index"
import ajax from '../../network/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    subjectCode: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    isShow: false,
    status: false,
    memo: "",
    image: [],
    showSub: false,
    list: [],
    selectVal: "",
    showTree: false,
    selectSubjectCode: null
  },
  lifetimes: {
    ready() {
      if (!this.data.subjectCode) return
      this.getChildren(this.data.subjectCode).then(async res => {
        let list = res.data.content
        if (list && list.length > 0) {
          let a = await this.getAllList(list);
          setTimeout(() => {
            let b = {}
            b['subjectName'] = '现金'
            b['subjectCode'] = this.data.subjectCode
            b['children'] = a
            b['id'] = Date.now()
            this.setData({
              list: b,
              showTree: true
            })
          }, 100);
        } else {
          this.setData({
            showTree: false
          })
        }
      })
    },
  },
  /**
   * 组件的方法列表
   */

  methods: {
    //事件处理函数
    move(){},
    tapItem: function (e) {
      this.setData({
        selectVal: e.detail.subjectName,
        subjectCode: e.detail.subjectCode,
        selectSubjectCode: e.detail.subjectCode,
        showSub: false
      })
    },
    //选择科目
    selectSub(e) {
      let item = e.currentTarget.dataset.item
      this.setData({
        selectVal: item.subjectName,
        subjectCode: item.subjectCode,
        showSub: false
      })
    },
    // 递归得到所有的科目选项
    async getAllList(list) {
      if (list && list.length > 0) {
        for (var i = 0; i < list.length; i++) {
          var s = await this.getChildrenFnc(list[i].subjectCode);
          if (s && s.length > 0) {
            // arr = arr.concat(s);
            list[i].children = s;
            this.getAllList(s);
          }
        }
      }
      return list;
    },
    // 获取子节点
    getChildren(subjectCode) {
      return new Promise((rs, rj) => {
        ajax({
          url: "/subject/subjectNextSubjectList",
          method: 'post',
          data: {
            subjectCode
          }
        }).then(res => {
          if (res.code == 200) {
            rs(res)
          } else {
            rj()
          }
        }).catch(() => {
          rj()
        })
      })
    },

    async getChildrenFnc(code) {
      return this.getChildren(code).then((res) => {
        if (
          res.code == 200 &&
          res.data &&
          res.data.content &&
          res.data.content.length > 0
        ) {
          return res.data.content;
        }
      });
    },


    clickSub() {
      this.setData({
        showSub: !this.data.showSub
      })
    },
    selectSubTwo() {
      this.setData({
        selectVal: '现金',
        subjectCode: this.data.subjectCode,
        selectSubjectCode: this.data.subjectCode,
        showSub: false
      })
    },
    delPicture(e) {
      //获取图片的下标
      let {
        index
      } = e.currentTarget.dataset
      let _this = this
      wx.showModal({
        title: "提示",
        content: "是否删除图片",
        success(res) {
          if (res.confirm) {
            let image = _this.data.image.filter((item, i) => {
              return !index == i
            })
            _this.setData({
              image
            })

            wx.showToast({
              title: '删除成功',
            })
          } else if (res.cancel) {
            wx.showToast({
              title: '取消删除',
              icon: "none"
            })
          }
        }
      })

    },
    //预览图片
    prePicture(e) {

      let current = e.currentTarget.dataset

      wx.previewImage({
        urls: this.data.image,
        current: current.image
      })
    },
    close() {
      this.triggerEvent('closePopup')
    },
    inputValue(e) {
      this.setData({
        memo: e.detail.value
      })
    },
    //发射事件
    submit(type) {
      let _this = this
      wx.showModal({
        title: '提示',
        content: '是否提交该流程',
        success(res) {
          if (res.confirm) {
            _this.triggerEvent('submit', {
              status: _this.data.status,
              memo: _this.data.memo,
              image: _this.data.image,
              subjectCode: _this.data.selectSubjectCode,
              type//1、同意  2、拒绝
            })
          } else if (res.cancel) {
            wx.showToast({
              title: '取消成功',
            })
          }
        }
      })

    },
    consent() {
      this.setData({
        status: true
      })
      this.submit(1)
    },
    refuse() {

      this.setData({
        status: false
      })
      this.submit(2)
    },
    uploadFile() {
      let _this = this
      let image = this.data.image
      //调用相机
      wx.chooseImage({
        count: 3,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFiles
          let ratio = 80
          tempFilePaths.forEach(element => {
            if (element.size > 20971520) {
              wx.showToast({
                title: '图片限制为20M',
                icon: "none"
              })
              return
            } else if (element.size > 10485760)
              ratio = 10
            else if (element.size > 5242880 && element.size < 10485760) {
              ratio = 20
            }

            wx.compressImage({
              src: element.path,
              quality: ratio,
              fail: function (v) {
                wx.showToast({
                  title: ' 图片上传失败',
                  icon: "none"
                })
              },
              success: function (v) {

                wx.showLoading({
                  title: '上传图片中',
                })
                wx.uploadFile({
                  filePath: v.tempFilePath,
                  name: 'img',
                  url: fileUrl,
                  complete: function () {
                    wx.hideLoading({
                      success: (res) => {},
                    })
                  },
                  success: function (v) {

                    if (v.statusCode == 200) {
                      let data = JSON.parse(v.data)

                      image.push(data.data)

                      _this.setData({
                        image
                      })
                    } else {
                      wx.showToast({
                        title: '上传失败',
                        icon: "none"
                      })
                    }

                  }
                })
              },
              fail(v) {
                wx.showToast({
                  title: '图片压缩失败',
                  icon: "none"
                })
              }
            })

          })
        }
      })

    }
  }
})